﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 台签打印
{
    public partial class Form2 : Form
    {
        readonly Setup Setup;

        public Form2(Setup setup)
        {
            InitializeComponent();
            Setup = setup;
            var fontSize = new[] { "10", "12", "14", "16", "18", "20", "22", "24", "28", "32" };
            var fontlist = FontHandle.Fonts;

            //提示
            var tip = new ToolTip();
            tip.SetToolTip(comboBox1, "输入文本内容，多列数据可选第一列或最后一列");
            tip.SetToolTip(comboBox2, "输入文本内容，多列数据可选第一列或最后一列");
            tip.SetToolTip(comboBoxFont1, "选择字体");
            tip.SetToolTip(comboBoxFont2, "选择字体");
            tip.SetToolTip(comboBoxSize1, "字体大小");
            tip.SetToolTip(comboBoxSize2, "字体大小");
            tip.SetToolTip(comboBoxStyle1, "字体样式");
            tip.SetToolTip(comboBoxStyle2, "字体样式");
            tip.SetToolTip(button2, "颜色");
            tip.SetToolTip(button3, "颜色");
            tip.SetToolTip(numericUpDownX1, "坐标位置");
            tip.SetToolTip(numericUpDownX2, "坐标位置");
            tip.SetToolTip(numericUpDownY1, "坐标位置");
            tip.SetToolTip(numericUpDownY2, "坐标位置");
            tip.SetToolTip(numericUpDown1, "边距百分比");
            tip.SetToolTip(numericUpDownX, "X坐标偏移");
            tip.SetToolTip(numericUpDownY, "Y坐标偏移");


            //位置偏移 1/2左右
            numericUpDownX.Maximum = Setup.Size.Width / 2;
            numericUpDownX.Minimum = Setup.Size.Width / 2 * -1;
            numericUpDownY.Maximum = Setup.Size.Height / 2;
            numericUpDownY.Minimum = Setup.Size.Height / 2 * -1;
            numericUpDown1.Value = (decimal)Setup.MarginRatio;
            if (Setup.Offset.X > numericUpDownX.Maximum || Setup.Offset.X < numericUpDownX.Minimum
                || Setup.Offset.Y > numericUpDownY.Maximum || Setup.Offset.Y < numericUpDownY.Minimum)
                Setup.Offset = new Point();
            numericUpDownX.Value = Setup.Offset.X;
            numericUpDownY.Value = Setup.Offset.Y;

            //抬头            
            comboBox1.Items.AddRange(new[] { "第一列" });
            comboBoxFont1.Items.AddRange(fontlist);
            comboBoxSize1.Items.AddRange(fontSize);
            comboBoxStyle1.Items.AddRange(Enum.GetNames(typeof(MyFontStyle)));
            comboBox1.Text = Setup.Title1.Content;
            comboBox1.ForeColor = Setup.Title1.Color1;
            comboBox1.Enabled = Setup.Title1.Enabled;
            checkBox1.Checked = Setup.Title1.Enabled;
            comboBoxFont1.Text = Setup.Title1.FontName;
            comboBoxSize1.Text = Setup.Title1.FontSize.ToString();
            comboBoxStyle1.Text = Setup.Title1.MyFontStyle.ToString();
            numericUpDownX1.Maximum = Setup.Size.Width;
            numericUpDownY1.Maximum = Setup.Size.Height;
            if (Setup.Title1.Point.IsEmpty || Setup.Title1.Point.X > Setup.Size.Width || Setup.Title1.Point.Y > Setup.Size.Height)
                Setup.Title1.Point = new Point((int)(Setup.Size.Width * 0.1), (int)(Setup.Size.Height * 0.1));
            numericUpDownX1.Value = Setup.Title1.Point.X;
            numericUpDownY1.Value = Setup.Title1.Point.Y;

            //落款
            comboBox2.Items.AddRange(new[] { "最后一列" });
            comboBoxFont2.Items.AddRange(fontlist);
            comboBoxSize2.Items.AddRange(fontSize);
            comboBoxStyle2.Items.AddRange(Enum.GetNames(typeof(MyFontStyle)));
            comboBox2.Text = Setup.Title2.Content;
            comboBox2.ForeColor = Setup.Title2.Color1;
            comboBox2.Enabled = Setup.Title2.Enabled;
            checkBox2.Checked = Setup.Title2.Enabled;
            comboBoxFont2.Text = Setup.Title2.FontName;
            comboBoxSize2.Text = Setup.Title2.FontSize.ToString();
            comboBoxStyle2.Text = Setup.Title2.MyFontStyle.ToString();
            numericUpDownX2.Maximum = Setup.Size.Width;
            numericUpDownY2.Maximum = Setup.Size.Height;
            if (Setup.Title2.Point.IsEmpty || Setup.Title2.Point.X > Setup.Size.Width || Setup.Title2.Point.Y > Setup.Size.Height)
                Setup.Title2.Point = new Point((int)(Setup.Size.Width * 0.6), (int)(Setup.Size.Height * 0.85));
            numericUpDownX2.Value = Setup.Title2.Point.X;
            numericUpDownY2.Value = Setup.Title2.Point.Y;

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.Enabled = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            comboBox2.Enabled = checkBox2.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Setup.MarginRatio = (float)numericUpDown1.Value;
                Setup.Offset = new Point((int)numericUpDownX.Value, (int)numericUpDownY.Value);
                var fontStyle1 = (MyFontStyle)Enum.Parse(typeof(MyFontStyle), comboBoxStyle1.Text);
                Setup.Title1 = new Title
                {
                    Enabled = checkBox1.Checked,
                    Content = comboBox1.Text,
                    FontName = comboBoxFont1.Text,
                    FontSize = int.Parse(comboBoxSize1.Text),
                    MyFontStyle = fontStyle1,
                    Argb = comboBox1.ForeColor.ToArgb(),
                    Point = new Point((int)numericUpDownX1.Value, (int)numericUpDownY1.Value),
                };
                var fontStyle2 = (MyFontStyle)Enum.Parse(typeof(MyFontStyle), comboBoxStyle2.Text);
                Setup.Title2 = new Title
                {
                    Enabled = checkBox2.Checked,
                    Content = comboBox2.Text,
                    FontName = comboBoxFont2.Text,
                    FontSize = int.Parse(comboBoxSize2.Text),
                    MyFontStyle = fontStyle2,
                    Argb = comboBox2.ForeColor.ToArgb(),
                    Point = new Point((int)numericUpDownX2.Value, (int)numericUpDownY2.Value),
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Console.WriteLine(Setup);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var dialog = new ColorDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                comboBox1.ForeColor = dialog.Color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var dialog = new ColorDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                comboBox2.ForeColor = dialog.Color;
            }
        }

    }


}
